package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Dish;
import com.itheima.entity.Employee;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.entity.Category;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CacheManager cacheManager;


    //套餐的分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> selectByPage(Integer page, Integer pageSize, String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        Page<Setmeal> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Setmeal::getName,name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> page2 = setmealService.page(page1,qw);
        Page<SetmealDto> setmealDtoPage=new Page<>();
        BeanUtils.copyProperties(page2,setmealDtoPage,"records");
        //修改records
        List<Setmeal> list=page2.getRecords();
        List<SetmealDto> list1=new ArrayList<>();
        //遍历集合
        for (Setmeal s :
                list) {
            //拷贝集合
            SetmealDto setmealDto=new SetmealDto();
            //避免空指针异常
            Long id=s.getCategoryId();
            Category byId = categoryService.getById(id);
            log.info(id.toString());
            setmealDto.setCategoryName(byId.getName());
            BeanUtils.copyProperties(s,setmealDto);

            list1.add(setmealDto);
        }

        //设置page3
        setmealDtoPage.setRecords(list1);
        return R.success(setmealDtoPage);
    }

    //新增套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> saveSetmealDto(@RequestBody SetmealDto setmealDto){

        setmealService.saveSetmeal(setmealDto);

        return R.success("saasa");
    }

    //修改套餐状态
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> modifyStatus(@PathVariable Integer status,Long[] ids){
        log.info(ids.toString()+"  status::"+status);
        List<Setmeal> dishes=new ArrayList<>();
        for (Long id:
                ids) {
            Setmeal byId = setmealService.getById(id);
            byId.setStatus(status);
            dishes.add(byId);
        }
        setmealService.updateBatchById(dishes);
        log.info("修改后："+dishes.toString());
        return R.success("修改成功！");
    }

    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(Long[] ids){
        log.info(ids.toString());
        for (Long id:
                ids) {
            setmealService.removeById(id);
        }
        return R.success("删除成功！");
    }

    //修改套餐的数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.selectById(id);
        return R.success(setmealDto);
    }

    //修改套餐的方法
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> modifySetmeal(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.modifySetmeal(setmealDto);
        return R.success("12");
    }

    //套餐的list方法
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public R<List<SetmealDto>> list(Long categoryId,Integer status){
        log.info(categoryId.toString()+":"+status);

        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        //查询分类下的套餐信息
        List<Setmeal> list = setmealService.list(qw);
        List<SetmealDto> list1=new ArrayList<>();

        for (Setmeal s:
             list) {
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(s,setmealDto);
            Long id = s.getId();
            LambdaQueryWrapper<SetmealDish> q1=new LambdaQueryWrapper<>();
            q1.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> list2 = setmealDishService.list();
            setmealDto.setSetmealDishes(list2);
            list1.add(setmealDto);
        }
        return R.success(list1);
    }

}
